package com.feilo.main;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JOptionPane;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class MainFrame {

	protected Shell shell;
	private Text txtPath;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			MainFrame window = new MainFrame();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		shell.setLocation(Display.getCurrent().getClientArea().width / 2 - shell.getShell().getSize().x/2, Display.getCurrent() 
                .getClientArea().height / 2 - shell.getSize().y/2); 
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("文件转换器");

		Label label = new Label(shell, SWT.NONE);
		label.setBounds(23, 68, 48, 17);
		label.setText("源文件：");

		txtPath = new Text(shell, SWT.BORDER);
		txtPath.setBounds(77, 65, 245, 23);

		Button btnImport = new Button(shell, SWT.NONE);
		btnImport.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog fileDialog = new FileDialog(shell, SWT.OPEN);
				fileDialog.setFilterNames(new String[] { "All Files (*.*)" });
				fileDialog.setFilterExtensions(new String[] { "*.*" });
				fileDialog.setFilterPath(System.getProperty("user.dir"));
				fileDialog.setText("请选择文件...");
				String dir = fileDialog.open();
				if (dir == null) {
					return;
				}
				txtPath.setText(dir);
			}
		});
		btnImport.setBounds(330, 63, 80, 27);
		btnImport.setText("导入文件");

		Button btnExport = new Button(shell, SWT.NONE);
		btnExport.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String path = txtPath.getText();
				if(path==null || "".equals(path)){
					JOptionPane.showMessageDialog(null, "请导入相应文件！","提示", JOptionPane.INFORMATION_MESSAGE);
					return ;
				}
				String suffix = path.substring(path.lastIndexOf('.') + 1, path.length());
				String text = ParseText.parse(path, suffix);
				String fileName = path.substring(path.lastIndexOf('\\') + 1,path.lastIndexOf('.'));
				FileDialog fd = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
				fd.setFileName(fileName);
				fd.setFilterExtensions(new String[] {"*.txt"});
				String result = fd.open();
				if (result != null && !"".equals(result)) {
					try {
						BufferedWriter bw = new BufferedWriter(new FileWriter(result));
						bw.write(text);
						bw.flush();
						bw.close();
						JOptionPane.showMessageDialog(null, "文件导出成功！","提示", JOptionPane.INFORMATION_MESSAGE);
					} catch (FileNotFoundException e1) {
						e1.printStackTrace();
					} catch (IOException e2) {
						e2.printStackTrace();
					}
				}
			}
		});
		btnExport.setBounds(55, 162, 135, 42);
		btnExport.setText("导出文件");

		Button btnExit = new Button(shell, SWT.NONE);
		btnExit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.exit(0);
			}
		});
		btnExit.setBounds(253, 162, 135, 42);
		btnExit.setText("退    出");

	}
}
